/**
 * @author 黄波
 * 创建日期：2011Sep 4, 2011
 * TODO// N/A
 */
package com.platform.manager.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.platform.manager.bo.UserGroupPowerBo;
import com.platform.pojo.GroupPower;

//配置用户组权限
public class ProUserGroupPowerAction extends ManagerSupport {

	private String groupId;
	private String[] chk;	//权限ID组
	/**
	 * @return the groupId
	 */
	public String getGroupId() {
		return groupId;
	}
	/**
	 * @param groupId the groupId to set
	 */
	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}
	@Override
	public String execute() throws Exception {
		String msg = null;
//		chks = ServletActionContext.getRequest().getParameterValues("chk");
		if(groupId == null || groupId.equals("")) {
			msg = "请选择用户组！";
		} else if (chk == null || chk.length == 0) {
			msg = "请选择权限！";
		} else {
			//取得该用户组的所有权限
			UserGroupPowerBo bo = new UserGroupPowerBo();
			List<GroupPower> powers = bo.getPowers(groupId);
			if(chk.length > powers.size()) {
				//选择的权限比目前数据库中保存的权限多，则在比对后进行添加
				for(String id : chk) {
					if(!contains(id, powers)) {
						GroupPower groupPower = new GroupPower();
						groupPower.setGroupId(Integer.parseInt(groupId));
						groupPower.setPowerId(Integer.parseInt(id));
						if(bo.addUserGroupPower(groupPower)) {
							msg = "分配成功！";
						} else {
							msg = "分配失败，数据库错误！";
						}
					}
				}
			} else if(chk.length < powers.size()) {
				//选择的权限比目前数据库中保存的权限，则在比对后进行删除
				for(GroupPower power : powers) {
					if(!contains(power.getPowerId(), chk)) {
						if(bo.deleteGroupPower(power)) {
							msg = "分配成功！";
						} else {
							msg = "分配失败，数据库错误！";
						}
					}
				}
			} else {
				//选择的权限比目前数据库中保存的权限多，则在比对后进行添加
				for(String id : chk) {
					if(!contains(id, powers)) {
						GroupPower groupPower = new GroupPower();
						groupPower.setGroupId(Integer.parseInt(groupId));
						groupPower.setPowerId(Integer.parseInt(id));
						if(bo.addUserGroupPower(groupPower)) {
							msg = "分配成功！";
						} else {
							msg = "分配失败，数据库错误！";
						}
					}
				}
				//选择的权限比目前数据库中保存的权限，则在比对后进行删除
				for(GroupPower power : powers) {
					if(!contains(power.getPowerId(), chk)) {
						if(bo.deleteGroupPower(power)) {
							msg = "分配成功！";
						} else {
							msg = "分配失败，数据库错误！";
						}
					}
				}
			}
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.flush();
		out.write("<script>alert('" + msg + "');history.back(1);</script>");
		out.close();
		return null;
	}
	/**
	 * @param powerId
	 * @param chks2
	 * @return
	 */
	private boolean contains(Integer powerId, String[] chks) {
		for(String chk : chks) {
			if(Integer.parseInt(chk) == powerId) {
				return true;
			}
		}
		return false;
	}
	/**
	 * @param id
	 * @param powers
	 * @return
	 */
	private boolean contains(String id, List<GroupPower> powers) {
		for(GroupPower item : powers) {
			if(id.equals(item.getPowerId().toString())) {
				return true;
			}
		}
		return false;
	}
	/**
	 * @return the chk
	 */
	public String[] getChk() {
		return chk;
	}
	/**
	 * @param chk the chk to set
	 */
	public void setChk(String[] chk) {
		this.chk = chk;
	}
}
